import random
import time
import tkinter


root=tkinter.Tk()
root.title('2201点名系统')
root.resizable(False,False)
title=tkinter.Label(root,text='欢迎使用课堂随机点名系统',font=("宋体",25),bg='white')
title.pack(pady=20)
root.config(bg='white')

def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size) 
center_window(root,800,300)

def chushihua():
    global names
    global select
    names=['卫新宇','王尚熙','宁  钰','黄天龙','张奥玮','刘思如','王京晶','林怡彤','时培杰','杨柳炜','袁紫涵','张卓琳','孙艺丹','郭泽坤','樊思晨','马梓航','崔梦垚','林钲皓','高梓萌','李嘉图','靳昊润','张巧巧','董晓泰','商妍茹','毕昭阳','胡川琪','张颢译','景元瑞','柴嘉晨','梁靖怡','许薰元','郭亚鑫','程相杰','尤常凯','王浩宇','马佳敏','李金阳','宋文晖','李季泽','弓士森','杨子轩','上官佐','胡梦涛','宁一张','宁一弛','张宸坤']
    select=[]
chushihua()
name=tkinter.StringVar()
name.set('请选择人数')

num=0
key=0
t=0

def startname():
        global i
        global name
        global names
        global t
        global selectlist
        global select
        i=0
        starttime = time.time()
        namedel()
        if num>10:
              names.append('吕承玺')
        jian.config(state='disabled')
        jia.config(state='disabled')
        start.config(state='disabled')
        while num!=0:
            time.sleep(0.1)
            name.set(random.choice(names))
            endtime = time.time()
            if i!=num:
                select.append(name.get())
                names.remove(name.get())
                t+=1
                i+=1
            if endtime-starttime>3:
                 break
            root.update()
        jian.config(state='normal')
        jia.config(state='normal')
        start.config(state='normal')
        if num!=0 and num>1:
            name.set('请点击“开始”进行点名')
            newwindow()
        elif num==0:
             name.set('请选择人数')
        else:
             name.set('，'.join(select))
        

def namedel():
        global selectlist
        global t
        global names
        global suiji
        global key
        root.after_cancel
        chushihua()
        selectlist.config(state=tkinter.NORMAL)
        t=0
        selectlist.delete(0.1,tkinter.END)
        selectlist.config(state=tkinter.DISABLED)

def gettime():
      timestr = time.strftime("%H:%M:%S")
      lb.configure(text=timestr)
      root.after(1000,gettime)

def numplus():
        global num
        global geshu
        global name
        if num<47:
            num+=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jian.config(state=tkinter.NORMAL)
        if num==47:
             jia.config(state=tkinter.DISABLED)
        if num!=0:
            name.set('请点击“开始”进行点名')

def numminus():
        global num
        global geshu
        global jian
        global jia
        if num>0:
            num-=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jia.config(state=tkinter.NORMAL)
        if num==0:
            jian.config(state=tkinter.DISABLED)
            name.set('请选择人数')
        if num!=0:
            name.set('请点击“开始”进行点名')

def newwindow():
    root = tkinter.Tk()
    root.title('名单')
    root.config()
    root.resizable(False,False)

    def center_window(root, width, height):
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(size) 
    
    if len(select)==2:
        center_window(root,260,40)
    elif len(select)==3:
        center_window(root,400,40)
    elif len(select)==4:
        center_window(root,540,40)
    elif len(select)==5:
        center_window(root,670,40)
    else:
        if len(select)%5!=0:
            center_window(root,710,(len(select)//5)*35+35)
        else:
            center_window(root,710,(len(select)//5)*35)

    text='，'.join(select)
    selectlist=tkinter.Text(root,font=("宋体",25,'bold'),relief=tkinter.FLAT,bg='white')
    selectlist.pack(expand=True)
    scrollbar=tkinter.Scrollbar(root,command=selectlist.yview,bg='white')
    scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y,before=selectlist)
    selectlist.config(yscrollcommand=scrollbar.set)

    selectlist.insert(tkinter.END,text)
    selectlist.config(state=tkinter.DISABLED)

    root.mainloop()


mingzi=tkinter.Frame(root,bg='white')
suiji=tkinter.Label(mingzi,textvariable=name,font=("宋体",30,'bold'),relief=tkinter.FLAT,bg='white')
suiji.pack(side=tkinter.LEFT,padx=150,pady=30)
selectlist=tkinter.Text(mingzi,font=("宋体",20,'bold'),relief=tkinter.FLAT,width=24, height=5,state=tkinter.DISABLED,bg='white')
scrollbar=tkinter.Scrollbar(mingzi,command=selectlist.yview,bg='white')
selectlist.config(yscrollcommand=scrollbar.set)
mingzi.pack(pady=20)


anniu=tkinter.Frame(root,bg='white')
renshu=tkinter.Label(anniu,text='人数:',font=("宋体",20,'bold'),bg='white')
renshu.pack(side=tkinter.LEFT,padx=50)
jian=tkinter.Button(anniu,text='-',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numminus,state=tkinter.DISABLED)
jian.pack(side=tkinter.LEFT)
geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
geshu.pack(side=tkinter.LEFT)
jia=tkinter.Button(anniu,text='+',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numplus)
jia.pack(side=tkinter.RIGHT)
start=tkinter.Button(anniu,text='开始',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=startname)
start.pack(side=tkinter.RIGHT,padx=50,before=jia)
anniu.pack()


lb = tkinter.Label(root,text='',font=("宋体",15),bg='white')
lb.pack(side=tkinter.LEFT)
gettime()


shuiyin=tkinter.Label(root,text='Fengchunye',font=("宋体",15),bg='white')
shuiyin.pack(side=tkinter.RIGHT)


root.mainloop()